'use strict';
const appId = ''
const secret = ''
const clientID = ''
const sk = ''
const ttAppId = ''
const ttSecret = ''
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	console.log('context : ', context)
	let env = context.platform
	let code = event.code
	let url =
		'https://api.weixin.qq.com/sns/jscode2session?appid=' +
		appId +
		'&secret=' +
		secret +
		'&js_code=' +
		code +
		'&grant_type=authorization_code'
	if (env === 'swan') {
		url = 'https://spapi.baidu.com/oauth/jscode2sessionkey?client_id=' + clientID + '&sk=' + sk + '&code=' +
			code
	}
	if (env === 'tt') {
		url =
			'https://developer.toutiao.com/api/apps/jscode2session?appid=' + ttAppId + '&secret=' + ttSecret +
			'&code=' + code
	}
	const res = await uniCloud.httpclient.request(url, {
		method: 'GET',
		data: {},
		dataType: 'json'
	})
	console.log(res)
	if (Object.prototype.hasOwnProperty.call(res.data, 'openid')) {
		return res.data
	} else {
		return res.data
	}
};